using System;
using System.Data;
using System.Configuration;
using Pheonix.Fashionzoomer.DAL;
using System.Data.SqlClient;
using System.Collections;
using Pheonix.CodeLibrary.Data;
using Pheonix.CodeLibrary;

/// <summary>
/// Summary description for Profile
/// </summary>

namespace Pheonix.Fashionzoomer.Entity.SqlServer2000
{
    [System.ComponentModel.DataObject]
    public class Profile : SqlServerDAO
    {
        #region Profile Private Fields

		private Int32 _id;
		private String _userFirstName;
		private String _userLastName;
		private Int32 _areaId;
		private Int32 _timeZone;
		private String _selectedTheme;
		private Int32 _userId;

        #endregion

        #region Profile Constractors

		public Profile(){}

		public Profile
		(
			Int32 id,
			String userFirstName,
			String userLastName,
			Int32 areaId,
			Int32 timeZone,
			String selectedTheme,
			Int32 userId

		)
		{
			this._id = id;
			this._userFirstName = userFirstName;
			this._userLastName = userLastName;
			this._areaId = areaId;
			this._timeZone = timeZone;
			this._selectedTheme = selectedTheme;
			this._userId = userId;
		}

        #endregion

        #region Profile Properties

		public Int32 Id
		{
			get{ return this._id; }
			set{ this._id = value; }
		}

		public String UserFirstName
		{
			get{ return this._userFirstName; }
			set{ this._userFirstName = value; }
		}

		public String UserLastName
		{
			get{ return this._userLastName; }
			set{ this._userLastName = value; }
		}

		public Int32 AreaId
		{
			get{ return this._areaId; }
			set{ this._areaId = value; }
		}

		public Int32 TimeZone
		{
			get{ return this._timeZone; }
			set{ this._timeZone = value; }
		}

		public String SelectedTheme
		{
			get{ return this._selectedTheme; }
			set{ this._selectedTheme = value; }
		}

		public Int32 UserId
		{
			get{ return this._userId; }
			set{ this._userId = value; }
		}


        #endregion

        #region Profile Static Methods

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Insert, true)]
        public static Int32 CreateNewProfile
        (
			String userFirstName,
			String userLastName,
			Int32 areaId,
			Int32 timeZone,
			String selectedTheme,
			Int32 userId

        )
        {
            //creating an empty Profile object
            Profile profile = new Profile();
            profile.UserFirstName = userFirstName;
			profile.UserLastName = userLastName;
			profile.AreaId = areaId;
			profile.TimeZone = timeZone;
			profile.SelectedTheme = selectedTheme;
			profile.UserId = userId;

            return Profile.CreateNewProfile(profile);
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
        public static bool UpdateProfile
        (
			Int32 id,
			String userFirstName,
			String userLastName,
			Int32 areaId,
			Int32 timeZone,
			String selectedTheme,
			Int32 userId

        )
        {
            Profile profile = Profile.GetProfileById(id);
            profile.UserFirstName = userFirstName;
			profile.UserLastName = userLastName;
			profile.AreaId = areaId;
			profile.TimeZone = timeZone;
			profile.SelectedTheme = selectedTheme;
			profile.UserId = userId;

            return Profile.UpdateProfile(profile);
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
        public static bool DeleteProfile
        (
            Int32 id
        )
        {
            //Validate Input
            if (id <= GetIdMinValue)
                throw (new ArgumentOutOfRangeException("id in Profile"));

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            AddParameterToSqlCmd(sqlCmd, "@Id", SqlDbType.Int, 0, ParameterDirection.Input, id);
            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_PROFILE_DELETEPROFILE);
            GetOneData(sqlCmd);
            int returnValue = (int)sqlCmd.Parameters["@ReturnValue"].Value;
            return (returnValue == 0 ? false : true);

        }

        public static bool DeleteProfileList(System.Collections.Generic.List<Int32> idListToDelete)
        {
            //Validate Input
            foreach (Int32 id in idListToDelete)
                if (id <= GetIdMinValue)
                    throw (new ArgumentOutOfRangeException("id in Profile"));

            //performing deletion operation //

            string csvString = Utility.MakeCSV(idListToDelete);

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            AddParameterToSqlCmd(sqlCmd, "@IdList", SqlDbType.NVarChar, csvString.Length, 
                ParameterDirection.Input, csvString);
            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_PROFILE_DELETEPROFILELIST);
            GetOneData(sqlCmd);
            int returnValue = (int)sqlCmd.Parameters["@ReturnValue"].Value;
            return (returnValue == idListToDelete.Count ? true : false);
        }
		
		[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, false)]
		public static CustomCollection<Profile> GetProfileByAreaId(Int32 areaId)
		{
			if(areaId <= GetIdMinValue)
				throw (new ArgumentOutOfRangeException("areaId in Profile"));

			SqlCommand sqlCmd = new SqlCommand();
			AddParameterToSqlCmd(sqlCmd, "@AreaId", SqlDbType.Int, 0, ParameterDirection.Input, areaId);
			SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_PROFILE_GETPROFILEBYAREAID);
			GenerateCollectionFromReader dg = new GenerateCollectionFromReader(GenerateProfileCollectionFromReader);
			CustomCollection<Profile> objCollection = ((CustomCollection<Profile>)GetAllData(sqlCmd, dg));

			if (objCollection.Count > 0)
				return objCollection;
			else
				return null;
		}

		[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, false)]
		public static CustomCollection<Profile> GetProfileByUserId(Int32 userId)
		{
			if(userId <= GetIdMinValue)
				throw (new ArgumentOutOfRangeException("userId in Profile"));

			SqlCommand sqlCmd = new SqlCommand();
			AddParameterToSqlCmd(sqlCmd, "@UserId", SqlDbType.Int, 0, ParameterDirection.Input, userId);
			SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_PROFILE_GETPROFILEBYUSERID);
			GenerateCollectionFromReader dg = new GenerateCollectionFromReader(GenerateProfileCollectionFromReader);
			CustomCollection<Profile> objCollection = ((CustomCollection<Profile>)GetAllData(sqlCmd, dg));

			if (objCollection.Count > 0)
				return objCollection;
			else
				return null;
		}

        #endregion

        #region Profile Instance Methods
        
        public bool Save()
		{
			if(Id <= GetIdMinValue)
			{
				Int32 TempId=CreateNewProfile(this);
				if(TempId>0)
				{
					_id=TempId;
					return true;
				}
				else
				{
					// error occurs
					return false;
				}
			}
			else
			{	//This is a update Command
				return (UpdateProfile(this));
			}
		}

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public static CustomCollection<Profile> GetAllProfile()
        {
            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_PROFILE_GETALLPROFILE);
            GenerateCollectionFromReader result = new GenerateCollectionFromReader(GenerateProfileCollectionFromReader);
            CustomCollection<Profile> objCollection = ((CustomCollection<Profile>)GetAllData(sqlCmd, result));

            return objCollection;
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public static CustomCollection<Profile> GetAllProfilePaged(String orderBy, Int32 startRowIndex, Int32 maximumRows)
        {
            //Validate Input
            if (orderBy == "")
                orderBy = "Id";

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@orderby", SqlDbType.VarChar, 50, ParameterDirection.Input, orderBy);
            AddParameterToSqlCmd(sqlCmd, "@startRowIndex", SqlDbType.Int, 0, ParameterDirection.Input, startRowIndex);
            AddParameterToSqlCmd(sqlCmd, "@maximumRows", SqlDbType.Int, 0, ParameterDirection.Input, maximumRows);

            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_PROFILE_GETALLPROFILEPAGED);

            GenerateCollectionFromReader result = new GenerateCollectionFromReader(GenerateProfileCollectionFromReader);
            CustomCollection<Profile> objCollection = ((CustomCollection<Profile>)GetAllData(sqlCmd, result));
            return objCollection;
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public static int GetAllProfilePagedCount(String orderBy, Int32 startRowIndex, Int32 maximumRows)
        {
            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();

            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_PROFILE_GETALLPROFILEPAGEDCOUNT);
            GetOneData(sqlCmd);

            int returnValue = (int)sqlCmd.Parameters["@ReturnValue"].Value;
            return returnValue;
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public static Profile GetProfileById(Int32 id)
        {
            if (id <= GetIdMinValue)
                throw (new ArgumentOutOfRangeException("id in Profile"));

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@Id", SqlDbType.Int, 0, ParameterDirection.Input, id);
            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_PROFILE_GETPROFILEBYID);
            GenerateCollectionFromReader result = new GenerateCollectionFromReader(GenerateProfileCollectionFromReader);
            CustomCollection<Profile> objCollection = ((CustomCollection<Profile>)GetAllData(sqlCmd, result));

            if (objCollection.Count > 0)
                return (Profile)objCollection[0];
            else
                return null;
        }

        public static Int32 CreateNewProfile(Profile newProfile)
        {
            // validate Parameters
            if (newProfile == null)
                throw (new ArgumentNullException("newProfile in Profile"));

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);

            AddParameterToSqlCmd(sqlCmd, "@UserFirstName", SqlDbType.NVarChar, 32, ParameterDirection.Input, newProfile.UserFirstName);
			AddParameterToSqlCmd(sqlCmd, "@UserLastName", SqlDbType.NVarChar, 32, ParameterDirection.Input, newProfile.UserLastName);
			AddParameterToSqlCmd(sqlCmd, "@AreaId", SqlDbType.Int, 0, ParameterDirection.Input, newProfile.AreaId);
			AddParameterToSqlCmd(sqlCmd, "@TimeZone", SqlDbType.Int, 0, ParameterDirection.Input, newProfile.TimeZone);
			AddParameterToSqlCmd(sqlCmd, "@SelectedTheme", SqlDbType.NVarChar, 16, ParameterDirection.Input, newProfile.SelectedTheme);
			AddParameterToSqlCmd(sqlCmd, "@UserId", SqlDbType.Int, 0, ParameterDirection.Input, newProfile.UserId);

            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_PROFILE_CREATENEWPROFILE);
            GetOneData(sqlCmd);
            return ((Int32)sqlCmd.Parameters["@ReturnValue"].Value);

        }

        public static bool UpdateProfile(Profile newProfile)
        {
            // validate Parameters
            if (newProfile == null)
                throw (new ArgumentNullException("newProfile"));

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);

            AddParameterToSqlCmd(sqlCmd, "@Id", SqlDbType.Int, 0, ParameterDirection.Input, newProfile.Id);
            AddParameterToSqlCmd(sqlCmd, "@UserFirstName", SqlDbType.NVarChar, 32, ParameterDirection.Input, newProfile.UserFirstName);
			AddParameterToSqlCmd(sqlCmd, "@UserLastName", SqlDbType.NVarChar, 32, ParameterDirection.Input, newProfile.UserLastName);
			AddParameterToSqlCmd(sqlCmd, "@AreaId", SqlDbType.Int, 0, ParameterDirection.Input, newProfile.AreaId);
			AddParameterToSqlCmd(sqlCmd, "@TimeZone", SqlDbType.Int, 0, ParameterDirection.Input, newProfile.TimeZone);
			AddParameterToSqlCmd(sqlCmd, "@SelectedTheme", SqlDbType.NVarChar, 16, ParameterDirection.Input, newProfile.SelectedTheme);
			AddParameterToSqlCmd(sqlCmd, "@UserId", SqlDbType.Int, 0, ParameterDirection.Input, newProfile.UserId);

            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_PROFILE_UPDATEPROFILE);
            GetOneData(sqlCmd);
            int returnValue = (int)sqlCmd.Parameters["@ReturnValue"].Value;
            return (returnValue == 0 ? false : true);
        }

        #endregion

        #region Profile Related Collections
        /// <summary>
        /// Creates and returns a strongly typed collection of Profile custom entity. 
        /// The colelction is created through iterating on the IdataReader object which contains 
        /// Profile information, as a set of records, similar to tabular format.
        /// </summary>
        public static IList GenerateProfileCollectionFromReader(IDataReader returnData)
        {

            //creating the instance of Profile collection
            CustomCollection<Profile> colProfile = new CustomCollection<Profile>();

            //Iterating thriugh the data reader, to generate Profile collection.

            //getting the Profile collection by iterating thru the data reader
            while (returnData.Read())
            {
                //passing the Profile constructor parameters from the current instance of data reader fields.
                Profile newProfile = new Profile
                (
                    returnData["id"] == System.DBNull.Value ? GetIdMinValue : (Int32)returnData["id"],
                    returnData["user_first_name"] == System.DBNull.Value ? String.Empty : (String)returnData["user_first_name"],
					returnData["user_last_name"] == System.DBNull.Value ? String.Empty : (String)returnData["user_last_name"],
					returnData["area_id"] == System.DBNull.Value ? Int32.MinValue : (Int32)returnData["area_id"],
					returnData["time_zone"] == System.DBNull.Value ? Int32.MinValue : (Int32)returnData["time_zone"],
					returnData["selected_theme"] == System.DBNull.Value ? String.Empty : (String)returnData["selected_theme"],
					returnData["user_id"] == System.DBNull.Value ? Int32.MinValue : (Int32)returnData["user_id"]

                );
                //adding the Profile to the collection
                colProfile.Add(newProfile);
            }

            //returns the collection of Profile 
            return (colProfile);
        }

        #endregion

        #region Constants and Default Values

        public static Int32 GetIdMinValue
        {
            get
            {
                return 0;
            }
        }

        //contants
        internal const string SPR_PROFILE_CREATENEWPROFILE = "spr_Profile_CreateNewProfile";
        internal const string SPR_PROFILE_UPDATEPROFILE = "spr_Profile_UpdateProfile";
        internal const string SPR_PROFILE_DELETEPROFILE = "spr_Profile_DeleteProfile";
        internal const string SPR_PROFILE_DELETEPROFILELIST = "spr_Profile_DeleteProfileList";
        internal const string SPR_PROFILE_GETPROFILEBYID = "spr_Profile_GetProfileById";
        internal const string SPR_PROFILE_GETALLPROFILE = "spr_Profile_GetAllProfile";
        internal const string SPR_PROFILE_GETALLPROFILEPAGED = "spr_Profile_GetAllProfilePaged";
        internal const string SPR_PROFILE_GETALLPROFILEPAGEDCOUNT = "spr_Profile_GetAllProfilePagedCount";

		//Foreign key contants
		internal const string SPR_PROFILE_GETPROFILEBYAREAID="spr_Profile_GetProfileByAreaId";
		internal const string SPR_PROFILE_GETPROFILEBYAREAIDPAGED="spr_Profile_GetProfileByAreaIdPaged";
		internal const string SPR_PROFILE_GETPROFILEBYAREAIDPAGEDCOUNT="spr_Profile_GetProfileByAreaIdPagedCount";
		internal const string SPR_PROFILE_GETPROFILEBYUSERID="spr_Profile_GetProfileByUserId";
		internal const string SPR_PROFILE_GETPROFILEBYUSERIDPAGED="spr_Profile_GetProfileByUserIdPaged";
		internal const string SPR_PROFILE_GETPROFILEBYUSERIDPAGEDCOUNT="spr_Profile_GetProfileByUserIdPagedCount";
		
        #endregion
    }
}
